//
//  GoodsAdressVC.m
//  EJLX
//
//  Created by adc on 15/11/27.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "GoodsAdressVC.h"
#import "GoodsAdress.h"
#import "QueRenDingDanViewController.h"
@interface GoodsAdressVC ()

@end

@implementation GoodsAdressVC
-(void)viewWillAppear:(BOOL)animated{
    [HttpsManager getAddressBlock:^(NSMutableArray *addressArray) {
        self.array = addressArray;
        __weak GoodsAdressVC *weakSelf = self;
        [_tableview addPullToRefreshWithActionHandler:^{
            NSLog(@"当进行下拉刷新执行该方法");
            [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
        }];
        
        [_tableview addInfiniteScrollingWithActionHandler:^{
            [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
        }];

        
        [_tableview reloadData];
 }];

}
- (void)requestEnd{
    [_tableview.pullToRefreshView stopAnimating];
    
    [_tableview.infiniteScrollingView stopAnimating];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatNav];
    
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDE, HEIGHT-64) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:_tableview];

   

    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   Address *address = [self.array objectAtIndex:indexPath.row];
    self.people.name=address.name;
    self.people.phone=address.phone;
    self.people.address=address.address;
    self.people.addressId=[NSString stringWithFormat:@"%d",address.addressID];
    self.mainVc.people=self.people;
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserTableViewCell *cell = nil;
    if (!cell) {
        cell = [[UserTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell5"];
        cell = [[NSBundle mainBundle] loadNibNamed:@"UserTableViewCell" owner:nil options:nil][4];

    }
    if (self.array) {
        Address *address = [self.array objectAtIndex:indexPath.row];
        NSLog(@"adress %@ %@ %@",address.name,address.phone,address.address);
       
            cell.addressNameLB.text = address.name;

        if (![address.phone isKindOfClass:[NSNull class]]) {
            cell.addressPhoneLB.text = address.phone;

        }

     cell.addressDetailLB.text = address.address;

    }
    
   
    
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)creatNav{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    [leftBtn setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60, 7, 120, 30  )];
    title.text = @"收货地址";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [navView addSubview:title];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(WIDE-50, 7, 30, 30);
    rightBtn.layer.cornerRadius = 20;
    [rightBtn setTitle:@"新增"  forState:UIControlStateNormal] ;
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:12.f];
    [rightBtn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:rightBtn];
    
}
-(void)backBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)add{
    GoodsAdress *adress = [[GoodsAdress alloc] init] ;
    [self.navigationController pushViewController:adress animated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
